layui.use(['table', 'form', 'laydate', 'layer'], function () {
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var createDate1;
    var createDate2;
    //渲染时间输入框
    laydate.render({
        elem: '#createDate1',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm:ss',
        theme: 'grid',
        done: function (value, date) {
            createDate1 = value;
        }
    });
    laydate.render({
        elem: '#createDate2',
        format: 'yyyy-MM-dd HH:mm:ss',
        type: 'datetime',
        theme: 'grid',
        done: function (value, date) {
            createDate2 = value;
        }
    });
    //第一个实例
    table.render({
        elem: '#test'
        , even: true
        , id: 'testReload'
        , url: 'jay/OutBound/getOutBoundList' //数据接口
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": 0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.obj.total, //解析数据长度
                "data": res.obj.list //解析数据列表
            };
        }
        , toolbar: '#bar'
        , defaultToolBar: ['default', 'print', 'export']
        , limit: 10
        , even: true
        , page: true //开启分页
        , cols: [[ //表头
            { field: 'id', title: '序号', align: 'center', width: 80, sort: true, fixed: 'left' }
            , { field: 'typeName', title: '单据类型', align: 'center', width: 160 }
            , {
                field: 'userName', title: '出库单审核人', align: 'center', width: 160, templet: function (obj) {
                    if (obj.staffId == 0) {
                        return "暂无";
                    } else {
                        return obj.userName;
                    }
                }
            }
            , { field: 'createDate', title: '创建时间', align: 'center', width: 160 }
            , { field: 'obStatusName', title: '出库单状态', align: 'center', width: 160 }
            , { fixed: 'right', title: '操作', align: 'center', toolbar: '#barDemo' }
        ]]
    });
    var $ = layui.$, active = {
        reload: function () {
            table.reload('testReload', {
                page: {
                    curr: 1//默认从第一页开始加载
                },
                where: {
                    type: $('#chukuType').val(),
                    status: $('#statusList').val(),
                    createDate1: createDate1,
                    createDate2: createDate2
                }
            });
        }
    }
    $('#find').on('click', function () {
        var type = 'reload';
        active[type] ? active[type].call(this) : '';
    });
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'detail') {
            layer.open({
                type: 2,
                area: ['1000px', '600px'],
                content: ['chukuDetail.html', 'yes'],
                title: '出库详情',
                maxmin: true,
                success: function (layero, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    //调用子页面的全局函数
                    iframe.relo(data.id, data.status);
                }
            });
        } else if (obj.event == 'del') {
            if (data.status == 1 || data.status == 2) {//审核未通过或者待审核的出库单都可退回
                layer.confirm("确认要退回出库单并清空记录吗?", { btn: ["确认", "取消"] },
                    function () {
                        $.ajax({
                            url: 'OutBound/deleteOutbound',
                            data: 'id=' + data.id + '&orderId=' + data.sourceId + "&type=" + data.type,
                            success: function (res) {
                                if (res.obj != null) {
                                    layer.msg("退回成功", { icon: 1 });
                                    setTimeout(function () {
                                        location.href = 'addOrder.html';
                                    }, 500);
                                    location.href = "chuku.html";
                                } else {
                                    layer.msg("退回失败，请稍后再试", { icon: 2 });
                                }
                            }
                        });
                    }, function () {
                        layer.msg("您取消了操作", { icon: 2 });
                    })
            } else {
                layer.msg("退回失败，仅能退回待审核的出库单", { icon: 2 });
            }
        } else if (obj.event == 'order' && data.type == 1) {
            layer.msg("抱歉，盘亏出库单没有单据来源，请勿持续点击", { icon: 2 });
        } else if (obj.event == 'addPick') {
            layer.open({
                type: 2,
                area: ['600px', '380px'],
                content: 'picker.html',
                success: function (layero, index) {
                    var iframe = window['layui-layer-iframe' + index];
                    //调用子页面的全局函数
                    iframe.getChuku(data);
                }
            });
        }
    });
    $.ajax({
        url: 'jay/OutBound/getTypeList',
        success: function (data) {
            var data = data.obj;
            for (var i = 0; i < data.length; i++) {
                $("#chukuType").append("<option value=\"" + data[i].id + "\">" + data[i].typeName + "</option>");
            }
            form.render("select");
        }
    });
    $.ajax({
        url: 'jay/OutBound/getStatusList2',
        success: function (data) {
            var data = data.obj;
            for (var i = 0; i < data.length; i++) {
                $("#statusList").append("<option value=\"" + data[i].id + "\">" + data[i].obStatusName + "</option>");
            }
            form.render("select");
        }
    });
});